<?php
	@session_start ();
	include("includes/secure.php");
	if ( $_SESSION['use_type'] != 'Admin')
		echo "<script> window.location.href='logout.php'; </script>";
	
	include("includes/config.php");
	include("includes/class/user.class.php");
	require("includes/class/RijndaelHex.php");
		
	$user = new User(); 
	$rijndaelHex = new RijndaelHex();
	
	$msg_info = $msg_alert = $msg_error = false;
	$_SESSION['msg_cool'] = '';
	$show_msg = 0;
	
	if(isset($_POST['add']) && $_POST['add'] == 1){
		$data->use_name = trim(mysql_real_escape_string($_POST['use_name']));
		$data->use_lastname = trim(mysql_real_escape_string($_POST['use_lastname']));
		$data->use_email = trim(mysql_real_escape_string($_POST['use_email']));
		$data->use_login = trim(mysql_real_escape_string($_POST['use_login']));
		$data->use_password = $rijndaelHex->linencrypthex( $data->use_login.mysql_real_escape_string($_POST['use_password']).$data->use_login );
		$data->use_status = trim(mysql_real_escape_string($_POST['use_status']));
		$data->use_type = trim(mysql_real_escape_string($_POST['use_type']));
		$data->tel_type = 'User';

		if($data->id_fk = $user->addUser($data)){
			$msg_info = true;
			$_SESSION["msg_cool"] .= 'El usuario fue adicionado satisfactoriamente!!.<br />';			
			
			$countTel_number = count ( $_POST['tel_number'] );
			$i = 0;
			while ( $i < $countTel_number ){
				$data->tel_number = $_POST['tel_number'][$i];
				$data->tel_ext = $_POST['tel_ext'][$i];
				
				if ( ! $user->addTelephone($data) ){
					$msg_alert = true;
					$_SESSION["msg_cool"] .= 'Error adicionando los telefonos del usuario.<br />';
				}
					
				$i ++;
			}
			echo "<script> window.location.href='listUser.php?show_msg=3'</script>";
		}
		else{
			$msg_error = true;
			$_SESSION["msg_cool"] .= 'Error adicionando el usuario.<br />';
		}
	}
	
	if ( $msg_error )
		$show_msg = 1;
	elseif ( $msg_alert )
		$show_msg = 2;
	elseif ( $msg_info )
		$show_msg = 3;
?>